/* 用法：
var c:AbstractClock=new AbstractClock();
c.setUI(ui,null,ui.minuteHand,ui.hourHand);
c.start(); */
 

package smlab.view.ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 时钟类
	 * @author simon
	 * 
	 */
	public class AbstractClock_SM extends Sprite
	{
		private var _ui:DisplayObject;
		private var _secondHand:DisplayObject;
		private var _minuteHand:DisplayObject;
		private var _hourHand:DisplayObject;
		private var _isRunning:Boolean=false;
		
		public function AbstractClock_SM()
		{
			super();
		}
		
		/**
		 * 设置皮肤
		 * @param ui
		 * @param secondHand
		 * @param minuteHand
		 * @param hourHand
		 * 
		 */
		public function setUI(ui:DisplayObject,secondHand:DisplayObject=null,minuteHand:DisplayObject=null,hourHand:DisplayObject=null):void
		{
			_ui=ui;
			_secondHand=secondHand;
			_minuteHand=minuteHand;
			_hourHand=hourHand;
			
			if(ui){
				addChild(_ui);
			}
		}
		
		public function start():void{
			if(!_ui || _isRunning==true){
				return;
			}
			addEventListener(Event.ENTER_FRAME,onEnterFrame$);
		}
		
		public function stop():void{
			removeEventListener(Event.ENTER_FRAME,onEnterFrame$);
			_isRunning=false;
		}
		
		private function onEnterFrame$(e:Event):void{
			var now:Date = new Date();
			var hourPart:Number = now.getHours();
			var minutesPart:Number = now.getMinutes();
			var secondsPart:Number = now.getSeconds();
			trace(hourPart+"时 "+minutesPart+"分 "+secondsPart+"秒");
			
			if(_secondHand){
				_secondHand.rotation = (secondsPart / 60) * 360;
			}
			
			if(_minuteHand){
				_minuteHand.rotation = (minutesPart / 60) * 360;
			}
			
			if(_hourHand){
				_hourHand.rotation = (hourPart/12) * 360;
			}
		}
		
	}
}